//
//  POPDatePickViewController.m
//  Runone
//
//  Created by runone01 on 16/9/8.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "POPDatePickViewController.h"
//#import "constructionViewController.h"
#import "RODateHelper.h"
@interface POPDatePickViewController ()
@property (weak, nonatomic) IBOutlet UIDatePicker *datePicker;
@property(nonatomic,copy)NSString *dateString;
@end

@implementation POPDatePickViewController

#pragma      ----------日历选择器

- (void)viewDidLoad {
    [super viewDidLoad];
    self.preferredContentSize=CGSizeMake(300, 257);
     self.datePicker.backgroundColor = [UIColor groupTableViewBackgroundColor];
     [self.datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged ];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//设置为中文显示
    self.datePicker.locale     = locale;
    self.datePicker.timeZone   = [NSTimeZone systemTimeZone];;
    _datePicker.datePickerMode = UIDatePickerModeDate;
    //当前时间创建NSDate
    NSDate *localDate = [RODateHelper localeDate];
    //在当前时间加上的时间：格里高利历
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
    //设置时间
    [offsetComponents setYear:20];
    [offsetComponents setMonth:12];
    [offsetComponents setDay:5];
    [offsetComponents setHour:20];
    [offsetComponents setMinute:0];
    [offsetComponents setSecond:0];
    //设置最大值时间
    NSDate *maxDate = [gregorian dateByAddingComponents:offsetComponents toDate:localDate options:0];
    //设置属性
    NSString *minStr=@"1990-01-01";
    NSDateFormatter *fomater=[[NSDateFormatter alloc] init];
    [fomater setDateFormat:@"YYYY-MM-DD"];
    NSDate *min=[fomater dateFromString:minStr];
    self.datePicker.minimumDate = min;
    self.datePicker.maximumDate = maxDate;
    // Do any additional setup after loading the view from its nib.
}

-(void)dateChanged:(UIDatePicker *)sender{

    //NSDate格式转换为NSString格式
    NSDate *pickerDate = [sender date];// 获取用户通过UIDatePicker设置的日期和时间
    NSDateFormatter *pickerFormatter = [[NSDateFormatter alloc] init];// 创建一个日期格式器
    [pickerFormatter setDateFormat:@"yyyy-MM-dd  HH:mm:ss"];
    _dateString = [pickerFormatter stringFromDate:pickerDate];
    //打印显示日期时间
    NSLog(@"格式化显示时间：%@",_dateString);
    
}

- (IBAction)confirmBtn:(UIButton *)sender {
    
    if (_dateBlock!=nil) {
        _dateBlock(_dateString);
    }

    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (IBAction)cancle:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
